package com.example.ivos.user.service.impl;

import com.example.ivos.base.exception.ServiceException;
import com.example.ivos.base.response.StatusCode;
import com.example.ivos.user.pojo.dto.UserQuery;
import com.example.ivos.user.pojo.dto.UserSaveParam;
import com.example.ivos.user.pojo.entity.User;
import com.example.ivos.user.service.UserService;
import com.example.ivos.user.mapper.UserMapper;
import com.example.ivos.user.pojo.dto.UserLoginParam;
import com.example.ivos.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//业务层实现类上要加@Service注解，表示该类是业务层实现类
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录业务:userLoginParam={}",userLoginParam);
        String username=userLoginParam.getUsername();
        UserVO userVO = userMapper.login(username);
        if(userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userLoginParam.getPassword().equals(userVO.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void save(UserSaveParam userSaveParam) {
        log.debug("用户注册业务:userSaveParam={}",userSaveParam);
        log.debug("userSaveParam.getId()={}",userSaveParam.getId());
        log.debug("if (userSaveParam.getId() == null): {}", userSaveParam.getId() == null);
        User user=new User();
        BeanUtils.copyProperties(userSaveParam,user);
        log.debug("if (user.getId() == null): {}", user.getId() == null);
        if(user.getId()==null){ //前端没有传过来用户id，说明是新增用户
            user.setCreateTime(new Date());
            user.setPassword("123456");
            userMapper.save(user);
        }
        else{
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }


    @Override
    public void del(Long id) {
        userMapper.del(id);
    }

    @Override
    public void update(Long id) {
        User user=new User();
        user.setId(id);
        user.setPassword("123456");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public List<UserVO> select(UserQuery userQuery) {
        log.debug("用户查询业务:userQuery={}",userQuery);
        List<UserVO> userVOS = userMapper.UserSelect(userQuery);
        return userVOS;
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("业务层修改:id={},status={}",id,status);
        User user=new User();
        user.setId(id);
        user.setStatus(status);
        userMapper.update(user);
    }

    @Override
    public List<UserVO> selectAudit(Long parentId) {
        log.debug("查询审批人信息业务:parentId={}",parentId);
        ArrayList<UserVO> userVOList =new ArrayList<>();
        for(int i=0;i<2;i++){
            if(parentId==null){
                log.debug("parentId为空，退出循环");
                break;
            }
            else {
                UserQuery userQuery=new UserQuery();
                userQuery.setId(parentId);
                List<UserVO> userVOS = userMapper.UserSelect(userQuery);
                userVOList.addAll(userVOS);
                parentId=userVOS.get(0).getParentId();
            }
        }
        return userVOList;
    }
}
